import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import { get as getProjection } from 'ol/proj';
import WMTS from 'ol/source/WMTS';
import { getWidth, getTopLeft } from 'ol/extent';
import WMTSTileGrid from 'ol/tilegrid/WMTS';

class TDT {
    _init(type,option){

    }

    /**
     * 根据xyz方式请求切片
     * @param {*} url 
     * @param {*} options 
     * @returns 
     */
    static addByXYZ(url,options){
        const epsg = 'EPSG:4490';
        const projection = getProjection(epsg);
        const source = new XYZ({
            url,
            projection,
            ...options,
        });
        const layer = new TileLayer({
            source,
        });
        return layer;
    }

    /**
     * 
     * @returns 根据wmts请求切片
     */
    static addByWMTS(url,options){
        const epsg = 'EPSG:4490';
        const projection = getProjection(epsg);
        const projectionExtent = projection.getExtent();
        const size = getWidth(projectionExtent) / 256;
        const length = 19;
        const resolutions = new Array(length);
        const matrixIds = new Array(length);
        for (let i = 0; i < length; i += 1) {
            const pow = Math.pow(2, i);
            resolutions[i] = size / pow;
            matrixIds[i] = i;
        }
        const source = new WMTS({
            url: url,
            layer: 'img',
            style: 'default',
            matrixSet: 'c',
            format: 'tiles',
            wrapX: true,
            tileGrid: new WMTSTileGrid({
                origin: getTopLeft(projectionExtent),
                resolutions,
                matrixIds,
            }),
        });
        const layer = new TileLayer({
            source,
        });
        return layer;
    }

}

export default TDT